<template>
  <div class="new">
    <van-overlay :show="overlayShow" :z-index="1000" style="background-color: #ffffff">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>

    <!-- <van-nav-bar title="添加计划" left-arrow fixed placeholder safe-area-inset-top @click-left="goBack">
      <template #left>
        <van-icon name="arrow-left" color="#1b1b1b" size="25" />
      </template>
</van-nav-bar> -->

    <div>
      <van-collapse v-model="activeNames">
        <van-collapse-item size="large" v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <idatetime ishead :itemdata="itemdata" v-if="itemdata.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
              <icode ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'code'" />
              <ienum ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'enum'" :jsondata="jsondata" :idobject="idobject" />
              <iref ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'ref'" :ref="itemdata.sField" :clickmodel.sync="clickmodel" :clickfrom.sync="clickfrom" :clickindexName.sync="clickindexName" :clickindex.sync="clickindex" :jsondata="jsondata" :activeNames="activeNames" />
              <ibool ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'bool'" :jsondata="jsondata" :idobject="idobject" />
              <ifile ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'file'" :jsondata="jsondata" />
              <ipicture ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'picture'" :jsondata="jsondata" />
              <istring ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'string'" :jsondata="jsondata" :idobject="idobject" />
              <imap ishead :itemdata="itemdata" v-else-if="itemdata.sUIType == 'map'" :jsondata="jsondata" :item="item" />
              <istring ishead :itemdata="itemdata" v-else :jsondata="jsondata" :idobject="idobject" />
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>
    </div>

    <div class="cycle-plan">
      <div class="cycle-plan-top">
        <div>
          <span style="color:red;">*&nbsp;</span>周期计划:
        </div>
        <ul class="tab-list">
          <li class="tab-item" v-for="plan in cyclePlans" :key="plan.enumid" @click="changeTab(plan.enumid)" :class="{ 'active': activeTab === plan.enumid }">{{ plan.enumval }}</li>
        </ul>
      </div>
      <div class="cycle-plan-bottom">
        <div class="tab-content" v-if="activeTab === '2c9a2d9291db5a1f0191de90cbb60081'">
          <div class="tab-selected">
            <el-select v-model="selectedOption1" placeholder="请选择">
              <el-option v-for="item in schedulingTypes" :key="item.enumid" :label="item.enumval" :value="item.enumid">
              </el-option>
            </el-select>
            <el-select v-model="selectedOption2" multiple collapse-tags style="margin-left: 20px;" placeholder="请选择">
              <el-option v-for="item in this.options2" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
          <idatetime ishead :itemdata="this.endDate" v-if="this.endDate.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
          <van-checkbox v-model="checked" checked-color="rgb(20, 148, 245)" v-if="showCheckbox">{{ skipRestDay.sTitle }}</van-checkbox>
        </div>
        <div class="tab-content" v-if="activeTab === '2c9a2d9291db5a1f0191de90efd70082'">
          <idatetime ishead :itemdata="this.beginDate" v-if="this.beginDate.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
          <idatetime ishead :itemdata="this.endDate" v-if="this.endDate.sUIType == 'datetime'" :bzjsrqField.sync="bzjsrqField" :xzksrqField.sync="xzksrqField" :xzjsrqField.sync="xzjsrqField" />
        </div>
      </div>
    </div>

    <!-- 单独处理的部分 -->
    <div class="customer-info">
      <p><span>*&nbsp;</span>对应客户:</p>
      <button class="add-btn" @click="addCustomer()">添加</button>
    </div>
    <div v-if="selectedCustomers && selectedCustomers.length > 0" class="ulcontainer">
      <ul class="customer-list">
        <li :key="index" class="cus-item" :id="index" v-for="(item, index) in selectedCustomers">
          <div class="customer-details">
            <p class="title">{{ item.sname }}&nbsp;&nbsp;<span v-if="scenarioInfo.accountobj == 'contact'" style="color: gray;">{{ item.idaccount_ref }}</span></p>
            <!-- <p>{{item.saddress}}</p> -->
          </div>
          <van-icon class="del-btn" @click="delLi(index)" name="cross" style="margin-left:10px" />
          <!-- <button class="del-btn" @click="delLi(index)" >x</button> -->
        </li>
      </ul>
    </div>
    <div v-else class="empty-state">
      <svg width="64" height="41" viewBox="0 0 64 41" xmlns="http://www.w3.org/2000/svg">
        <g fill="none" fill-rule="evenodd" opacity=".5">
          <path d="M0 32h64v9H0z" fill="#F2F3F5" />
          <path d="M10 37h44v4H10z" fill="#FFF" />
          <g fill="#E0E0E0">
            <path d="M8 25h48v6H8zM10 0h44v22H10z" />
          </g>
          <path d="M19 10h10v3H19z" fill="#FFF" />
          <path d="M19 16h26v3H19z" fill="#F2F3F5" />
        </g>
      </svg>
      <p>暂无数据</p>
    </div>

    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item v-if="this.beforeSave && !this.afterSaveSucceed" @click="save">保存</van-tabbar-item>
      <van-tabbar-item v-if="isflow && this.beforeSave && !this.afterSaveSucceed" @click="savesubmit">保存并提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="submit">提交</van-tabbar-item>
      <van-tabbar-item v-if="isflow && !this.beforeSave && this.afterSaveSucceed" @click="audit">审核</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import {
  Search,
  List,
  Cell,
  PullRefresh,
  Toast,
  Tabbar,
  TabbarItem,
  Popup,
  DatetimePicker,
  ActionSheet,
  Tab,
  Tabs,
  Collapse,
  CollapseItem,
  CheckboxGroup,
  Checkbox,
  Uploader,
  Overlay,
  Loading,
  Rate,
  NumberKeyboard,
  Dialog,
  NavBar
} from 'vant'
import qs from 'qs'
import useInput from '@/utils/useinput'
import * as dd from 'dingtalk-jsapi'
Vue.use(NavBar).use(useInput).use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(Rate).use(NumberKeyboard)
export default {
  name: 'plannew',
  data () {
    let _this = this
    return {
      idobject: this.$route.query.idobject,
      idtemplate: this.$route.query.idtemplate,
      refCustomers: this.$route.query.mxlist,
      jsondata: {},
      index: 0,
      active: 0,
      activeNames: [],
      activeBodyNams: [''],
      isLoad: false,
      conditionValHidden: '',
      clickmodel: {},
      clickfrom: '',
      clickindexName: '',
      clickindex: 0,
      isDisable: false,
      overlayShow: true,
      notemessage: '',
      isflow: false,
      bzjsrqField: '',
      xzksrqField: '',
      xzjsrqField: '',
      solrLoading: true,
      solrFinished: false,
      solrList: [],
      solrListDivStyle: {
        display: 'none'
      },
      /* 用明细的account字段标识所需的参数 */
      detailAccount: '',

      beforeSave: true,
      afterSaveSucceed: false,
      activeTab: 'tab1',
      cyclePlan: {}, /* 周期计划 */
      cyclePlans: [], /* 周期计划值 */
      beginDate: '', /* 开始时间 */
      endDate: '', /* 结束时间 */
      schedulingType: '', /* 排期类型 */
      schedulingTypes: [], /* 排期类型值 */
      schedulingValue: '', /* 排期值 */
      skipRestDay: {}, /* 排期跳过休息日 */
      selectedOption1: '',
      selectedOption2: [],
      options2: [],
      checked: false,
      showCheckbox: false,

      scenarioid: this.$route.query.scenarioid, // 计划场景
      scenarioInfo: JSON.parse(this.$route.query.scenarioInfo) // 计划场景
    }
  },
  created: function () {
    //console.log('created......')
    let _this = this
    document.title = this.$route.query.stitle
    //console.log('document.title', document.title)
    if (dd.env.platform !== 'notInDingTalk') {
      dd.ready(function () {
        dd.biz.navigation.setTitle({
          title: _this.$route.query.stitle
        })
      })
    }
  },
  computed: {
    selectedCustomers () {
      return this.refCustomers || []
    }
  },
  mounted: function () {
    //console.log('this.refCustomers', this.refCustomers)
    //console.log('start created')
    //console.log('idobject', this.idobject)
    this.$store.state.ref_arr = null
    if (!this.isLoad) {
      let data = {
        idtemplate: this.idtemplate
      }
      let url = '/api/' + this.idobject + '.new.do'
      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(res => {
        //console.log('res:', res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.overlayShow = false
          this.notemessage = res.data.notemessage
          this.isflow = res.data.isflow
          this.jsondata = res.data.jsondata
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            let head = res.data.jsondata.head[i]
            this.activeNames.push(head.grouptitle)
            //console.log('head...', head)
            if (head && head.grouptitle === '计划管理-新建') {
              const headGroupValue = head.groupvalue
              for (let j = 0; headGroupValue && j < headGroupValue.length; j++) {
                const value = headGroupValue[j]
                if (value && value.sField === 'zqlx') {
                  this.cyclePlan = value
                } else if (value && value.sField === 'zqlxpq') {
                  this.schedulingType = value
                } else if (value && value.sField === 'pqz') {
                  this.schedulingValue = value
                } else if (value && value.sField === 'pqtgxxr') {
                  this.skipRestDay = value
                } else if (value && value.sField === 'kssj') {
                  this.beginDate = value
                } else if (value && value.sField === 'jssj') {
                  this.endDate = value
                } else if (value && value.sField === 'lxdjh') {
                  value.value = this.$route.query.code
                  continue
                } else if (value.sField === 'jhcj') {
                  value.value = this.scenarioInfo.sname
                  value.valuehidden = this.scenarioInfo.id
                  value.lCanInput = '0'
                  continue
                } else {
                  continue
                }
                headGroupValue.splice(j, 1)
                j--
              }
            }
          }
          const allenumidArr = this.schedulingType.allenumid.split(';')
          const allenumvalArr = this.schedulingType.allenumval.split(';')
          //console.log('length: ', allenumidArr)
          for (let i = 0; allenumidArr && i < allenumidArr.length; i++) {
            //console.log('allenumidArr[i]', allenumidArr[i])
            //console.log('allenumvalArr[i]', allenumvalArr[i])
            if (allenumidArr[i] && allenumvalArr[i]) {
              this.schedulingTypes.push({
                enumid: allenumidArr[i],
                enumval: allenumvalArr[i]
              })
            }
          }

          this.selectedOption1 = this.schedulingTypes[0].enumid

          const allenumidPlan = this.cyclePlan.allenumid.split(';')
          const allenumvalPlan = this.cyclePlan.allenumval.split(';')
          for (let i = 0; allenumidPlan && i < allenumidPlan.length; i++) {
            //console.log('allenumidPlan[i]', allenumidPlan[i])
            //console.log('allenumvalPlan[i]', allenumvalPlan[i])
            if (allenumidPlan[i] && allenumvalPlan[i]) {
              this.cyclePlans.push({
                enumid: allenumidPlan[i],
                enumval: allenumvalPlan[i]
              })
            }
          }

          this.activeTab = this.cyclePlans[0].enumid

          if (this.$route.query.activeTab) {
            this.activeTab = this.$route.query.activeTab
          }

          if (this.skipRestDay && (this.skipRestDay.value === '是' || this.skipRestDay.value === '1')) {
            this.checked = true
          }

          /* //console.log('this.hasjhcj && this.jhcj', this.hasjhcj, this.jhcj, this.jhcjitemdata, this.$refs)
          if (this.hasjhcj && !this.jhcj) {
            this.$refs.jhcj.openRef(this.jhcjitemdata, 'headdata', '', 0)
          } else if (this.hasjhcj && this.jhcj) {
            this.saveDetailAccount()
            this.hasjhcj = false
          } */
          this.saveDetailAccount()
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
    //console.log('this.refCustomers', this.refCustomers)
    //console.log('start created')
    //console.log('idobject', this.idobject)
  },
  activated () {
    //console.log('this.$store.state.ref_arr', this.$store.state.ref_arr)
    //console.log('this.refCustomers', this.refCustomers)
    if (this.$store.state.ref_arr) {
      //console.log('this.$store.state.ref_arr', this.$store.state.ref_arr)
      if (!this.refCustomers || this.refCustomers.length <= 0) {
        this.refCustomers = this.$store.state.ref_arr
        return
      }
      const newArr = this.$store.state.ref_arr.filter(itemA => {
        return this.refCustomers.every(itemB => {
          return itemB.id !== itemA.id
        })
      })
      this.refCustomers = this.refCustomers.concat(newArr)
    }

    let a = { aa: 'aaa' }
    //console.log('a1', a)
    //console.log('clickmodel1', this.clickmodel)
    //console.log('clickindex1', this.clickindex)
    //console.log('clickindexName1', this.clickindexName)
    //console.log('clickfrom1', this.clickfrom)
    if (this.$store.state.ref_show !== '') {
      this.clickmodel.value = this.$store.state.ref_show
      this.clickmodel.valuehidden = this.$store.state.ref_id
      this.clickmodel.errormsg = ''
      let fieldmap = this.clickmodel.fieldmap
      //console.log('fieldmap', fieldmap)
      if (fieldmap !== undefined && fieldmap.length > 0) {
        let arrFieldmap = fieldmap.split(';')
        let item = this.$store.state.ref_item
        if (this.clickfrom === 'headdata') {
          a.aa = 'bbb'
          for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
            let itemgroup = this.jsondata.head[groupindex]
            for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
              let itemdata = itemgroup.groupvalue[itemindex]
              for (let fieldmapindex in arrFieldmap) {
                let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
                if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                  continue
                }
                if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                  continue
                }
                if (itemdata.sField === arrFieldMapKey[1]) {
                  if (itemdata.sUIType === 'ref') {
                    itemdata.value = item[arrFieldMapKey[0] + '_ref']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else if (itemdata.sUIType === 'enum') {
                    itemdata.value = item[arrFieldMapKey[0] + '_enum']
                    itemdata.valuehidden = item[arrFieldMapKey[0]]
                  } else {
                    itemdata.value = item[arrFieldMapKey[0]]
                  }
                }
              }
            }
          }
        } else if (this.clickfrom === 'bodydata') {
          a.aa = 'ccc'
          for (let itemindex in this.jsondata.bodydata[this.clickindexName][this.clickindex]) {
            let itemdata = this.jsondata.bodydata[this.clickindexName][this.clickindex][itemindex]
            for (let fieldmapindex in arrFieldmap) {
              let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
              if (this.$store.state.ref_field === arrFieldMapKey[1]) {
                continue
              }
              if (arrFieldMapKey.length === 3 && arrFieldMapKey[2] !== '' && this.$store.state.ref_field !== arrFieldMapKey[2]) {
                continue
              }
              if (itemdata.sField === arrFieldMapKey[1]) {
                if (itemdata.sUIType === 'ref') {
                  itemdata.value = item[arrFieldMapKey[0] + '_ref']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else if (itemdata.sUIType === 'enum') {
                  itemdata.value = item[arrFieldMapKey[0] + '_enum']
                  itemdata.valuehidden = item[arrFieldMapKey[0]]
                } else {
                  itemdata.value = item[arrFieldMapKey[0]]
                }
              }
            }
          }
        }
      }
      //console.log('a', a)
      //console.log('clickmodel', this.clickmodel)
      //console.log('clickindex', this.clickindex)
      //console.log('clickindexName', this.clickindexName)
      //console.log('clickfrom', this.clickfrom)
      this.clickmodel = {}
      this.clickindex = 0
      this.clickindexName = ''
      this.clickfrom = ''
      this.$store.commit('clearRef')
    }
  },
  watch: {
    selectedOption1: function (newVal, oldVal) {
      //console.log('newVal:', newVal)
      //console.log('oldVal:', oldVal)
      if (newVal === '2c9a2d9291db5a1f0191de968601008e') {
        this.options2 = [
          { value: '星期一', label: '星期一' },
          { value: '星期二', label: '星期二' },
          { value: '星期三', label: '星期三' },
          { value: '星期四', label: '星期四' },
          { value: '星期五', label: '星期五' },
          { value: '星期六', label: '星期六' },
          { value: '星期日', label: '星期日' }
        ]
        this.showCheckbox = false
      } else if (newVal === '2c9a2d9291db5a1f0191de965ad6008d') {
        this.options2 = []
        for (let i = 1; i <= 31; i++) {
          this.options2.push({
            value: `${i}号`,
            label: `${i}号`
          })
        }
        this.showCheckbox = true
      }
      this.selectedOption2 = null
    }
  },
  methods: {
    goBack () {
      window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/')
    },
    changeTab (tab) {
      this.activeTab = tab
    },
    addCustomer () {
      //console.log('this.detailAccount', this.detailAccount)
      this.openRefMulti(this.detailAccount, 'headdata', '', 0)
    },
    openRefMulti (datamodel, datafrom, indexName, index) {
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindexName = indexName
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      //console.log('openRefMulti', datamodel)
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      //console.log('conditionValHidden', this.conditionValHidden)
      //console.log('datamodel....', datamodel)
      this.$router.push({
        name: 'refmulti',
        params: {
          bo: datamodel.idJoinObject,
          // bo: this.scenarioInfo.accountobj,
          tobo: datamodel.idObject,
          // tobo: this.scenarioInfo.idobject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: this.scenarioInfo.viewid ? this.scenarioInfo.viewid : datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          relbo: this.scenarioInfo.selectobj,
          reltobo: this.scenarioInfo.idobject
        }
      })
    },
    saveDetailAccount () {
      //console.log('saveDetailAccount', this.scenarioInfo)
      for (let indexName in this.jsondata.body) {
        for (let i = 0; i < this.jsondata.body[indexName].length; i++) {
          let item = this.jsondata.bodydata[indexName][i]
          for (let itemindex in item) {
            let itemdata = item[itemindex]
            if (itemdata.sField === 'idaccount' && this.scenarioInfo.accountobj === 'account') {
              this.detailAccount = itemdata
            } else if (itemdata.sField === 'idcontact' && this.scenarioInfo.accountobj === 'contact') {
              this.detailAccount = itemdata
            }
          }
        }
      }
    },
    delLi (indexToRemove) {
      this.refCustomers.splice(indexToRemove, 1)
      // Toast('删除成功!')
    },
    save () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }

        if (this.beginDate && this.activeTab === '2c9a2d9291db5a1f0191de90efd70082') {
          if (this.beginDate.lMustInput === '1' && this.beginDate.value === '') {
            this.beginDate.errormsg = '不能为空'
            cansave = 0
          }
          data[this.beginDate.idObject + '_' + this.beginDate.sField] = this.beginDate.value
        }

        if (this.endDate) {
          if (this.endDate.lMustInput === '1' && this.endDate.value === '') {
            this.endDate.errormsg = '不能为空'
            cansave = 0
          }
          data[this.endDate.idObject + '_' + this.endDate.sField] = this.endDate.value
        }

        //console.log('jsondata', this.jsondata)
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项!')
          return
        }

        if (this.refCustomers.length == 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请选择客户信息!')
          return
        }
        data.extrainfo = this.refCustomers.map(item => item.sname ? `${item.sname}` : 'null').join(';')
        data.khs = this.refCustomers.length
        data.customerids = this.refCustomers.map(item => item.id ? `${item.id}` : 'null').join(';')
        data.customeraddr = this.refCustomers.map(item => item.saddress ? `${item.saddress}` : 'null').join(';')

        if (this.cyclePlan) {
          data[this.cyclePlan.idObject + '_' + this.cyclePlan.sField] = this.activeTab
          data[this.cyclePlan.idObject + '_' + this.cyclePlan.sField + '_enum'] = ''
        }

        if (this.schedulingType) {
          data[this.schedulingType.idObject + '_' + this.schedulingType.sField] = this.selectedOption1
        }

        if (this.schedulingValue && this.selectedOption2) {
          data[this.schedulingValue.idObject + '_' + this.schedulingValue.sField] = this.selectedOption2.join(';')
        }

        if (this.skipRestDay && this.checked) {
          data[this.skipRestDay.idObject + '_' + this.skipRestDay.sField] = '1'
        }

        const routetype = this.$route.query.routetype

        if (routetype) {
          data['jhgl_jhscbylx'] = routetype
        }

        //console.log('data.......', data)
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          //console.log('newsave.do res', res)
          if (res.data.jsondata.audit == '0') {
            let data1 = {}
            data1[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']

            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.audit.do',
              data: qs.stringify(data1)
            }).then(res1 => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                this.refCustomers = []
                Toast('保存成功')
                if (this.$route.query.newflag === 'refnew') {
                  this.$store.commit('refReBack', { ref_show: 'true' })
                  this.$router.go(-1)
                  return
                }
                // this.$router.go(-1)
                // window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/');
                // this.$router.push({ name: 'jhgllist', query: { idobject: 'jhgl', stitle: '计划管理' } })
                this.$router.replace({ name: 'jhgllist', query: { idobject: 'jhgl', stitle: '计划管理' } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
            }).catch(res1 => {
              this.isDisable = false
              this.reserror(res)
            })
          } else {
            this.isDisable = false
            this.overlayShow = false
            if (res.data.flag === '0') {
              this.refCustomers = []
              Toast('保存成功')
              if (this.$route.query.newflag === 'refnew') {
                this.$store.commit('refReBack', { ref_show: 'true' })
                this.$router.go(-1)
                return
              }
              // this.$router.go(-1)
              // window.history.length > 1 ? this.$router.go(-1) : this.$router.push('/');
              // this.$router.push({ name: 'jhgllist', query: { idobject: 'jhgl', stitle: '计划管理' } })
              this.$router.replace({ name: 'jhgllist', query: { idobject: 'jhgl', stitle: '计划管理' } })
            } else {
              Toast(res.data.des)
              if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                this.loginout()
              }
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    submit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.flowsubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('提交成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    savesubmit () {
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        let cansave = 1
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                alert(itemdata.sTitle + '不能为空')
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              if (itemdata.lMustInput === '1' && itemdata.value === '') {
                itemdata.errormsg = '不能为空'
                cansave = 0
              }
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  if (itemdata.lMustInput === '1' && itemdata.value === '') {
                    itemdata.errormsg = '不能为空'
                    cansave = 0
                  }
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsave.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.isDisable = true
            this.overlayShow = true
            this.beforeSave = false
            this.afterSaveSucceed = true
            Toast('保存成功...开始提交...')
            // 发起提交请求开始 --
            this.axios({
              method: 'post',
              url: '/api/' + this.idobject + '.flowsubmit.do',
              data: qs.stringify(data)
            }).then(res => {
              this.isDisable = false
              this.overlayShow = false
              if (res.data.flag === '0') {
                Toast('提交成功')
                if (this.$route.query.newflag === 'refnew') {
                  this.$store.commit('refReBack', { ref_show: 'true' })
                  this.$router.go(-1)
                  return
                }
                this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              } else {
                Toast(res.data.des)
                if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
                  this.loginout()
                }
              }
              //console.log(res)
            }).catch(res => {
              this.isDisable = false
              this.overlayShow = false
              //console.log(res)
            })

            // 发起提交请求结束 --
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    }
  }
}
</script>

<style scoped>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}

div.customer-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.customer-info>p>span {
  color: red;
}

.ulcontainer {
  overflow-y: auto;
  margin-bottom: 70px;
}

.add-btn {
  background-color: transparent;
  color: rgba(39, 107, 202, 1);
  border: none;
  cursor: pointer;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 5px 15px;
  background-color: #eee;
}

.customer-list {
  list-style-type: none;
  padding: 12px 16px
}

.cus-item {
  display: flex;
  align-items: center;
  padding: 3px 20px;
}

.cus-item :nth-child(1) {
  marigin-top: 5px;
}

.customer-details {
  flex-grow: 1;
  border: 1px solid #eee;
  padding: 10px 16px;
  overflow: auto;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.customer-details .title {
  margin: 0;
  font-size: 16px;
  color: black;
}

.customer-details p {
  margin: 5px 0 0;
  font-size: 14px;
  color: #666;
}

.del-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(247, 123, 70, 1);
}

.input-group {
  margin-top: 15px;
  padding: 0 20px;
}

.input-group div {
  margin-top: 2px;
}

.input-group textarea {
  width: 100%;
  height: 120px;
  border: 1px solid #eee;
}

[class*=van-hairline]::after {
  border: none
}

.van-hairline--top-bottom::after,
.van-hairline-unset--top-bottom::after {
  border: none
}

.empty-state {
  text-align: center;
  padding: 50px 0;
  color: #999;
}

.empty-state svg {
  margin-bottom: 10px;
}

.empty-state p {
  font-size: 16px;
  color: #999;
}

.van-tabbar {
  display: flex;
  justify-content: center;
  padding-bottom: 15px !important;
}

.van-tabbar .van-tabbar-item {
  color: white;
  background-color: #5790df !important;
  width: 80% !important;
  flex: 0 0 80% !important;
  font-size: 14px;
  line-height: 14px;
}

.tab-list {
  list-style: none;
  display: flex;
  justify-content: flex-start;
}

.tab-item {
  cursor: pointer;
  padding: 5px 15px;
  margin-right: 25px;
  border: 1px solid #ccc;
  border-radius: 4px;
  color: rgba(39, 107, 202, 1);
}

.tab-item.active {
  /*background-color: #f0f0f0;*/
  background-color: rgba(88, 163, 240, 1);
  color: rgba(255, 255, 255, 1);
}

.tab-content {
  display: flex;
  padding: 20px 0px 20px 42px;
  flex-direction: column;
  justify-content: flex-start;
}

.tab-content.active {
  display: block;
}

.tab-content div {
  /*padding: 2px 0px;*/
}

.tab-selected {
  display: flex;
  justify-content: space-between;
}

.tab-selected select {
  width: 45%;
  height: 51px;
  padding: 10px 12px;
  line-height: 24px;
  border: 1px solid #eee;
  color: rgb(100, 101, 102);
  font: 400 16px "Avenir", Helvetica, Arial, sans-serif;
}

.cycle-plan {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  padding: 0 20px;
}

.cycle-plan .cycle-plan-top {
  display: flex;
  justify-content: flex-start;
  width: 100%;
  font: 400 16px "Avenir", Helvetica, Arial, sans-serif;
  color: rgb(100, 101, 102);
}

.cycle-plan .cycle-plan-top>div {
  width: 7em;
  padding: 5px 0px;
}

.cycle-plan-bottom {
  width: 100%;
}

.van-checkbox {
  padding: 10px 16px;
}

.van-checkbox span {
  font: 400 16px "Avenir", Helvetica, Arial, sans-serif;
  color: rgb(100, 101, 102);
}
</style>
